package lookie.app.ui.totd;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Window;
import java.util.Random;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.richclient.application.ApplicationServicesLocator;
import org.springframework.richclient.application.config.ApplicationObjectConfigurer;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.dialog.ApplicationDialog;

import com.l2fprod.common.swing.JTipOfTheDay;
import com.l2fprod.common.swing.TipModel;

public class TipOfTheDayBox {

	@Autowired
	private TipModelProvider tipModelProvider;

	private boolean showTipsSelected;
	
	private ApplicationObjectConfigurer objectConfigurer;

	public boolean isShowTipsSelected() {
		return showTipsSelected;
	}

	public void setShowTipsSelected(boolean showTipsSelected) {
		this.showTipsSelected = showTipsSelected;
	}
	
	public void setObjectConfigurer(ApplicationObjectConfigurer objectConfigurer) {
		this.objectConfigurer = objectConfigurer;
	}

	public ApplicationObjectConfigurer getObjectConfigurer() {
		if (objectConfigurer == null) {
			objectConfigurer = (ApplicationObjectConfigurer) ApplicationServicesLocator
					.services().getService(ApplicationObjectConfigurer.class);
		}
		return objectConfigurer;
	}

	public void display(Window parent) {
		TipOfTheDayDialog dialog = new TipOfTheDayDialog(parent, tipModelProvider.getTipModel());
		getObjectConfigurer().configure(dialog, "tipOfTheDayDialog");
		dialog.setShowTipsSelected(showTipsSelected);
		dialog.showDialog();
		showTipsSelected = dialog.isShowTipsSelected();
	}
	
	protected class TipOfTheDayDialog extends ApplicationDialog {

		private ActionCommand nextTipCommand;

		private ActionCommand prevTipCommand;

		private TipModel tipModel;

		private JCheckBox showTipsCheckBox;

		private boolean showTipsSelected;

		public TipOfTheDayDialog(Component parent, TipModel tipModel) {
			super("Tips", parent);
			this.tipModel = tipModel;
		}

		@Override
		protected String getFinishCommandId() {
			return "closeTipOfTheDayDialogCommand";
		}

		@Override
		protected JComponent createDialogContentPane() {

			JPanel panel = getComponentFactory().createPanel(new BorderLayout());
			final JTipOfTheDay tipOfTheDay = new JTipOfTheDay(tipModel);
			
			tipOfTheDay.setCurrentTip(new Random().nextInt(tipModel.getTipCount()));
			
			panel.add(tipOfTheDay);

			nextTipCommand = new ActionCommand("nextTipCommand") {

				@Override
				protected void doExecuteCommand() {
					tipOfTheDay.nextTip();
				}

			};

			prevTipCommand = new ActionCommand("prevTipCommand") {

				@Override
				protected void doExecuteCommand() {
					tipOfTheDay.previousTip();
				}

			};

			showTipsCheckBox = new JCheckBox("Show tips at startup", showTipsSelected);

			return panel;
		}

		@Override
		protected boolean onFinish() {
			showTipsSelected = showTipsCheckBox.isSelected();
			return true;
		}

		protected JComponent createButtonBar() {
			JComponent buttonBar = super.createButtonBar();
			JPanel panel = getComponentFactory().createPanel(
					new FlowLayout(FlowLayout.LEFT));
			panel.add(showTipsCheckBox);
			JPanel container = getComponentFactory()
					.createPanel(new BorderLayout());
			container.add(panel, BorderLayout.NORTH);
			container.add(buttonBar, BorderLayout.SOUTH);
			return container;
		}

		// protected JComponent createButtonBar() {
		// CommandGroup dialogCommandGroup = ButtonBarCommandGroup
		// .createCommandGroup(null, getCommandGroupMembers(), null);
		//
		// JComponent buttonBar = dialogCommandGroup.createButtonBar(Sizes
		// .bounded(Sizes.MINIMUM, Sizes.dluX(25), null));
		// GuiStandardUtils.attachDialogBorder(buttonBar);
		// return buttonBar;
		// }

		protected Object[] getCommandGroupMembers() {
			return new Object[] { prevTipCommand, nextTipCommand,
					getFinishCommand() };
		}

		public boolean isShowTipsSelected() {
			return showTipsSelected;
		}

		public void setShowTipsSelected(boolean showTipsSelected) {
			this.showTipsSelected = showTipsSelected;
		}
		
	}

}
